package com.jucai.pppeiyouclient.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.listener.CustomListener;
import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.bean.CardBean;
import com.jucai.pppeiyouclient.bean.PayTypeBean;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.LoginUtils;
import com.jucai.pppeiyouclient.utils.ToastUtils;
import com.jucai.pppeiyouclient.weidget.GetVerTextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class WithdrawsActivity extends BaseActivity {
    private EditText withdrawsSum, withdrawsPhone, withdrawsInputVer;
    private TextView withdrawsChooseCard, withdrawsSubmit;
    private GetVerTextView withdrawsGetVer;

    private PayTypeBean payTypeBean;
    private OptionsPickerView pvCustomOptions;
    private ArrayList<CardBean> cardItem = new ArrayList<>();
    private String carName;
    private String carNumber;
    private double dPrice;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_withdraws);
        ((TextView) findViewById(R.id.common_title_text)).setText("提现");
        withdrawsSum = (EditText) findViewById(R.id.withdraws_sum);
        withdrawsPhone = (EditText) findViewById(R.id.withdraws_phone);
        withdrawsInputVer = (EditText) findViewById(R.id.withdraws_input_verification);
        withdrawsChooseCard = (TextView) findViewById(R.id.withdraws_choose_card);
        withdrawsGetVer = (GetVerTextView) findViewById(R.id.withdraws_get_verification);
        withdrawsSubmit = (TextView) findViewById(R.id.withdraws_submit);
    }

    @Override
    protected void initData() {
        payTypeBean = (PayTypeBean) getIntent().getSerializableExtra("cars");

        pvCustomOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                carName = cardItem.get(options1).getPickerViewText();
                carNumber = cardItem.get(options1).getId();
                withdrawsChooseCard.setText(carName);
            }
        })
                .setLayoutRes(R.layout.pickerview_custom_options, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        TextView ivCancel = (TextView) v.findViewById(R.id.iv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomOptions.returnData();
                            }
                        });
                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomOptions.dismiss();
                            }
                        });

                    }
                })
                .isDialog(true)
                .build();
        getCardData();
        pvCustomOptions.setPicker(cardItem);
    }

    private void getCardData() {
        for (int i = 0; i < payTypeBean.getCredit_info().size(); i++) {
            PayTypeBean.CreditInfoBean bean = payTypeBean.getCredit_info().get(i);
            String showName = payTypeBean.getCredit_info().get(i).getCredit_card();
            if (!TextUtils.isEmpty(payTypeBean.getCredit_info().get(i).getCredit_card()) && payTypeBean.getCredit_info().get(i).getCredit_card().length() > 10)
                showName = payTypeBean.getCredit_info().get(i).getCredit_card().substring(payTypeBean.getCredit_info().get(i).getCredit_card().length() - 4, payTypeBean.getCredit_info().get(i).getCredit_card().length());

            if (i == 0) {
                carName = bean.getCredit_personinfo() + "(" + showName + ")";
                carNumber = bean.getCredit_card();
                withdrawsChooseCard.setText(carName);
            }
            CardBean cardBean = new CardBean(bean.getCredit_card(), bean.getCredit_personinfo() + "(" + showName + ")");
            cardItem.add(cardBean);
        }
    }

    @Override
    protected void loadNetData() {

    }

    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
        withdrawsChooseCard.setOnClickListener(this);
        withdrawsGetVer.setOnClickListener(this);
        withdrawsInputVer.setOnClickListener(this);
        withdrawsSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;

            case R.id.common_home_back:
                startActivity(new Intent(WithdrawsActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;

            case R.id.withdraws_choose_card:
                pvCustomOptions.show();
                break;

            case R.id.withdraws_get_verification:
                if (TextUtils.isEmpty(withdrawsPhone.getText().toString())) {
                    ToastUtils.showToast("手机号不能为空");
                } else {
                    //...
                }
                break;

            case R.id.withdraws_submit:
                if (TextUtils.isEmpty(carNumber)) {
                    ToastUtils.showToast("银行卡不能为空");
                    break;
                }
                String money = withdrawsSum.getText().toString().trim();
                if (!TextUtils.equals(money,"")) {
                    try {
                        dPrice = Double.parseDouble(money);
                    } catch (Exception e) {
                        e.printStackTrace();
                        ToastUtils.showToast("请输入正确的金额");
                        break;
                    }
                    outMoneyToCar();
                } else {
                    ToastUtils.showToast("充值金额不能为空");
                }
                break;
        }
    }

    private void outMoneyToCar() {
        Map<String, String> mapRegister = new HashMap<>();
        mapRegister.put("service", "Client_Wallet.Withdraw");
        mapRegister.put("credit", dPrice + "");
        mapRegister.put("withdraw_cardno", carNumber);
        mapRegister.put("withdraw_type", "chinapay");
        mapRegister.put("member_id", LoginUtils.getMember_id(this));
        mapRegister.put("token", LoginUtils.getToken(this));
        CommonRequestNetData.getJsonStringData(this, mapRegister, true, new CommonRequestNetData.JsonStringCallBack() {
            @Override
            public void parseJsonString(String str) throws Exception {
                ToastUtils.showToast(str);
                WithdrawsActivity.this.setResult(RESULT_OK);
                WithdrawsActivity.this.finish();
            }
        });

    }
}
